﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Web;
using MensUnoShop.Models;

namespace MensUnoShop.Controllers
{
    public class TradeAnget
    {
        public bool DoTrade(Member m, ref Models.Order od)
        {
            PayFactory pf = new PayFactory();
            bool istrade = false;
            string cashid = string.Empty;
            string paywayc=string.Empty,paywaye=string.Empty,paywaytitle=string.Empty;
            switch (od.PW)
            { 
                case Models.PayType.CreditCard:
                    REDIRECTURL = pf.DoCreditcardTrade(od.TXID, od.PAYPRICE, ref cashid);
                    od.CASHID = cashid;
                    istrade = true;
                    paywayc = "線上刷卡";
                    paywaye = "Credit card online";
                    paywaytitle = "線上刷卡Credit card online";
                    break;
                case Models.PayType.CS:
                    REDIRECTURL = pf.DoCSTrade(od.TXID, od.PAYPRICE, od.TXID, DateTime.Today.AddDays(3),  m.LASTNAME+m.FIRSTNAME, m.MOBILE, od.PW, ref cashid);
                    od.CASHID = cashid;
                    istrade = true;
                    paywayc = "超商代收";
                    paywaye = "cash on the E-commerce machine of convenient stores";
                    paywaytitle = "超商代收Payment at convenient stores";
                    break;
                case Models.PayType.ATM:
                    REDIRECTURL = pf.DoCSTrade(od.TXID, od.PAYPRICE, od.TXID, DateTime.Today.AddDays(3), m.LASTNAME + m.FIRSTNAME, m.MOBILE, od.PW, ref cashid);
                    od.CASHID = cashid;
                    istrade = true;
                    paywayc = "ATM轉帳 (虛擬帳號)";
                    paywaye = "Virtual account";
                    paywaytitle = "ATM轉帳";
                    break;
                case Models.PayType.WebATM:
                    REDIRECTURL = pf.DoCSTrade(od.TXID, od.PAYPRICE, od.TXID, DateTime.Today.AddDays(3), m.LASTNAME + m.FIRSTNAME, m.MOBILE, od.PW, ref cashid);
                    od.CASHID = cashid;
                    istrade = true;
                    paywayc = "Web-ATM";
                    paywaye = "Web-ATM";
                    paywaytitle = "web-atm";
                   break;
               case Models.PayType.MMK:
                   REDIRECTURL = pf.DoCSTrade(od.TXID, od.PAYPRICE, od.TXID, DateTime.Today.AddDays(3), m.LASTNAME + m.FIRSTNAME, m.MOBILE, od.PW, ref cashid);
                    od.CASHID = cashid;
                    istrade = true;
                    paywayc = "簡單付";
                    paywaye = "ezPay";
                    paywaytitle = "簡單付ezPay";
                    break;
            }

            StringBuilder mlist = new StringBuilder();
            foreach (var mk in od.MLIST)
            { 
                mlist.Append("<tr><td><div align=\"center\"><img src=\""+mk.IMG+"\" width=\"75\" height=\"75\" /></div></td>");
                mlist.Append("<td valign=\"top\">"+mk.SN+"<br />"+mk.TITLE+"</td><td><div align=\"center\">NT$"+mk.SINGLEPRICE+"</div></td>");
                mlist.Append("<td><div align=\"center\">" + mk.AMOUNT + "</div></td><td><div align=\"center\">NT$" + mk.PRICE + "</div></td></tr>");
            }
            //發送通知信
            string mailcontent = File.ReadAllText(HttpContext.Current.Server.MapPath(Config.Latter_Mail_Order)).Replace("[lastname]", m.LASTNAME);
            mailcontent = mailcontent.Replace("[paywayc]", paywayc).Replace("[paywaye]", paywaye);
            mailcontent = mailcontent.Replace("[shipping]", Config.ShippingWay[od.SHIPPINGWAY]).Replace("[deliverycost]",od.DELIVERYCOST.ToString()).Replace("[price]", od.PAYPRICE.ToString());
            mailcontent = mailcontent.Replace("[name]", od.RECIPIENT.NAME).Replace("[address]", od.RECIPIENT.ADDRESS).Replace("[tel]", od.RECIPIENT.TEL);
            mailcontent = mailcontent.Replace("[mlist]", mlist.ToString());
            string error;
            Config.SendMail("訂購確認通知 Notification of your order("+paywaytitle+")", mailcontent, m.EMAIL, m.FIRSTNAME + "," + m.LASTNAME);
            return istrade;
        }

        public string REDIRECTURL { get; set; }
    }
}
